<template>
  <div>
    <BasicTable @register="registerTable" @edit-change="handleEditChange">
      <template #action="{ record, column }">
        <TableAction :actions="createActions(record, column)" />
      </template>
    </BasicTable>
    <a-button block class="mt-5" type="dashed" @click="handleAdd"> 新增成员 </a-button>
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { getorgtree, getposlist } from '../../system-api';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    ActionItem,
    EditRecordRow,
  } from '/@/components/Table';

  const columns: BasicColumn[] = [
    {
      title: '附属机构',
      dataIndex: 'orgName',
      edit: true,
      editComponent: 'ApiTreeSelect',
      editRule: false,
      editComponentProps: {
        api: getorgtree,
        replaceFields: {
          title: 'title',
          key: 'value',
          value: 'value',
        },
      },
    },
    {
      title: '附属岗位',
      dataIndex: 'posName',
      edit: true,
      editComponent: 'ApiSelect',
      editRule: false,
      editComponentProps: {
        api: getposlist,
        labelField: 'name',
        valueField: 'id',
      },
    },
  ];

  const data: any[] = [
    {
      title: 'John Brown',
      value: '00001',
    },
    {
      orgId: 'John Brown2',
      posId: '00002',
    },
  ];
  export default defineComponent({
    components: { BasicTable, TableAction },
    setup() {
      const [registerTable, { getDataSource, setTableData}] = useTable({
        columns: columns,
        showIndexColumn: false,
        //dataSource: data,
        actionColumn: {
          width: 100,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        pagination: false,
      });

      function handleEdit(record: EditRecordRow) {
        record.onEdit?.(true);
      }

      function handleCancel(record: EditRecordRow) {
        record.onEdit?.(false);
        if (record.isNew) {
          const data = getDataSource();
          const index = data.findIndex((item) => item.key === record.key);
          data.splice(index, 1);
        }
      }

      function handleSave(record: EditRecordRow) {
        record.onEdit?.(false, true);
        console.log(record);
      }

      function handleEditChange(data: Recordable) {
        console.log(data);
      }
      function setdatainfo(data: any) {
        //await handleAdd();
        setTableData(data);
      }
      function handleAdd() {
        const data = getDataSource();
        const addRow: EditRecordRow = {
          orgId: '',
          posId: '',
          editable: true,
          isNew: true,
        };
        data.push(addRow);
      }

      function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
        if (!record.editable) {
          return [
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              onClick: handleCancel.bind(null, record, column),
            },
          ];
        }
        return [
          {
            label: '保存',
            onClick: handleSave.bind(null, record, column),
          },
          {
            label: '取消',
            popConfirm: {
              title: '是否取消编辑',
              confirm: handleCancel.bind(null, record, column),
            },
          },
        ];
      }

      return {
        registerTable,
        handleEdit,
        createActions,
        handleAdd,
        getDataSource,
        handleEditChange,
        setdatainfo,
      };
    },
  });
</script>

